#region Header
//=============================================================================
//                          GShield (Game Shield)
//=============================================================================
//
// Original Programmer: Shawn Jackson
// Company: WaveTech Digital Technologies, Inc. (http://www.wtdt.com)
// Copyright: (c) 2006 WaveTech Digital Technologies, Inc. All Rights Reserved.
// Assembly: Engine
// Code File Name: EmailManager.cs
// Code File Date: 8/8/2007
//
// Code File Info: Email Management System
//=============================================================================
/*
 * UPDATES
 * -------
 *		0.9.6.x (ShawnJ) 12/24/2007: Moved from EntLib to Linq
 */
#endregion Header

#region License
/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#endregion License

#region Includes
using System;
using System.Net;
using System.Net.Mail;
using System.Text;
using WaveTech.GShield.Data;
using WaveTech.GShield.Model;

#endregion Includes

namespace WaveTech.GShield.Engine.Managers
{
	/// <summary>
	/// Manages the Email system for sending messages and
	/// notifications.
	/// </summary>
	public class EmailManager
	{
		#region Private Members
		private Setting m_settings;
		private SmtpClient m_client;
		#endregion Private Members

		#region Singleton
		private static readonly EmailManager instance = new EmailManager();
		#endregion Singleton

		#region Constructor
		public EmailManager()
		{
			m_settings = SettingsDAO.Get();
			m_client = new SmtpClient(m_settings.SmtpServer, 25);

			SetupMailClient();
		}
		#endregion Constructor

		#region Instance Property
		public static EmailManager Instance
		{
			get
			{
				return instance;
			}
		}
		#endregion Instance Property

		#region Setup Mail Client
		/// <summary>
		/// Configures the Smtp client
		/// </summary>
		private void SetupMailClient()
		{
			if (m_settings.SmtpRequireLogin.HasValue && m_settings.SmtpRequireLogin.Value)
			{
				NetworkCredential _authInfo = new NetworkCredential(m_settings.SmtpLogin, m_settings.SmtpPassword);
				m_client.UseDefaultCredentials = false;
				m_client.Credentials = _authInfo;

				m_client.DeliveryMethod = SmtpDeliveryMethod.Network;
			}
		}
		#endregion Setup Mail Client

		#region Send Email
		/// <summary>
		/// Sends a Smtp email message
		/// </summary>
		/// <param name="from">The From email address</param>
		/// <param name="to">The destionation email address</param>
		/// <param name="subject">Subject of the email</param>
		/// <param name="body">Body of the message</param>
		/// <param name="isHtml">Is the body in Html format or plain text</param>
		public void SendEmail(string from, string to, string subject, string body, bool isHtml)
		{
			MailMessage message = new MailMessage();

			message.From = new MailAddress(from);
			message.To.Add(new MailAddress(to));

			message.IsBodyHtml = isHtml;

			message.Subject = subject;
			message.Body = body;

			try
			{
				m_client.Send(message);
			}
			catch
			{ /* Eat Exceptios for now*/}
		}
		#endregion Send Email

		#region Send Banned Violation Message
		/// <summary>
		/// Sends a Banned Game Policy Violation message to the GShield administrator
		/// </summary>
		/// <param name="username">Username of the offender</param>
		/// <param name="timestamp">Timestamp of the occurance</param>
		/// <param name="gameName">Name of the violating game</param>
		public void SendBannedViolationMessage(string username, DateTime timestamp, string gameName)
		{
			StringBuilder message = new StringBuilder();

			message.Append("<br>");
			message.Append("Banned Game Policy Violation");
			message.Append("<br>");
			message.Append("<br>");
			message.Append("GShield has detected a Banned Game Policy violation.");
			message.Append("<br>");
			message.Append("Username: " + username);
			message.Append("<br>");
			message.Append("Game: " + gameName);
			message.Append("<br>");
			message.Append("Timestamp: " + timestamp);
			message.Append("<br>");
			message.Append("GShield has terminated the game in accordance with the policy. If this is in error<br>");
			message.Append("the user should be removed from the banned list for this game in the GShiled Manager.<br>");
			message.Append("<br>");
			message.Append("Thank You for using GShield!");

			SendEmail(m_settings.AdminEmail, m_settings.AdminEmail, "GShield: Banned Game Policy Violation", message.ToString(), true);
		}
		#endregion Send Banned Violation Message

		#region Send Rating Violation Message
		/// <summary>
		/// Sends a Ratings Policy Violation message to the GShield administrator
		/// </summary>
		/// <param name="username">Username of the offender</param>
		/// <param name="timestamp">Timestamp of the occurance</param>
		/// <param name="gameName">Name of the violating game</param>
		/// <param name="gameRating">ESRB Rating of the game</param>
		public void SendRatingViolationMessage(string username, DateTime timestamp, string gameName, RatingTypes gameRating)
		{
			StringBuilder message = new StringBuilder();

			message.Append("<br>");
			message.Append("Ratings Policy Violation");
			message.Append("<br>");
			message.Append("<br>");
			message.Append("GShield has detected an ESRB Ratings Policy violation.");
			message.Append("<br>");
			message.Append("Username: " + username);
			message.Append("<br>");
			message.Append("Game: " + gameName);
			message.Append("<br>");
			message.Append("Game Rating: " + gameRating.ToString());
			message.Append("<br>");
			message.Append("Timestamp: " + timestamp);
			message.Append("<br>");
			message.Append("GShield has terminated the game in accordance with the policy. If this is in error<br>");
			message.Append("the users ESRB Ratings Policy should be adjusted in the GShiled Manager.<br>");
			message.Append("<br>");
			message.Append("Thank You for using GShield!");

			SendEmail(m_settings.AdminEmail, m_settings.AdminEmail, "GShield: Ratings Policy Violation", message.ToString(), true);
		}
		#endregion Send Rating Violation Message

		#region Send Usage Violation Message
		/// <summary>
		/// Sends a Usage Policy Violation message to the GShield administrator
		/// </summary>
		/// <param name="username">Username of the offender</param>
		/// <param name="timestamp">Timestamp of the occurance</param>
		/// <param name="timePlayed">Total time played today for the user</param>
		public void SendUsageViolationMessage(string username, DateTime timestamp, int timePlayed)
		{
			StringBuilder message = new StringBuilder();

			message.Append("<br>");
			message.Append("Usage Policy Violation");
			message.Append("<br>");
			message.Append("<br>");
			message.Append("GShield has detected a Usage Policy violation.");
			message.Append("<br>");
			message.Append("Username: " + username);
			message.Append("<br>");
			message.Append("Total Time Played (Minutes): " + timePlayed);
			message.Append("<br>");
			message.Append("Timestamp: " + timestamp);
			message.Append("<br>");
			message.Append("GShield has terminated the game in accordance with the policy. If this is in error<br>");
			message.Append("the users playing time for the day should be adjusted in the GShiled Manager.<br>");
			message.Append("<br>");
			message.Append("Thank You for using GShield!");

			SendEmail(m_settings.AdminEmail, m_settings.AdminEmail, "GShield: Banned Game Policy Violation", message.ToString(), true);
		}
		#endregion Send Usage Violation Message

		#region Send Time Violation Message
		/// <summary>
		/// Sends a Time Policy Violation message to the GShield administrator
		/// </summary>
		/// <param name="username">Username of the offender</param>
		/// <param name="timestamp">Timestamp of the occurance</param>
		public void SendTimeViolationMessage(string username, DateTime timestamp)
		{
			StringBuilder message = new StringBuilder();

			message.Append("<br>");
			message.Append("Time Policy Violation");
			message.Append("<br>");
			message.Append("<br>");
			message.Append("GShield has detected a Time Policy violation.");
			message.Append("<br>");
			message.Append("Username: " + username);
			message.Append("<br>");
			message.Append("Timestamp: " + timestamp);
			message.Append("<br>");
			message.Append("GShield has terminated the game in accordance with the policy. If this is in error<br>");
			message.Append("the user should have this Time of Day (Blackout) times modified in the GShiled Manager.<br>");
			message.Append("<br>");
			message.Append("Thank You for using GShield!");

			SendEmail(m_settings.AdminEmail, m_settings.AdminEmail, "GShield: Time Policy Violation", message.ToString(), true);
		}
		#endregion Send Time Violation Message
	}
}